<template>
  <div class="outfit-card" @click="onClick">
    <!-- 搭配图片 -->
    <div class="card-image-wrapper">
      <img 
        :src="outfit.coverImage || defaultCoverImage" 
        :alt="outfit.name" 
        class="card-image"
      />
      <!-- 季节标签 -->
      <div v-if="outfit.season" class="season-badge">
        {{ outfit.season }}
      </div>
      <!-- 收藏按钮 -->
      <div class="favorite-button" @click.stop="toggleFavorite">
        <van-icon 
          :name="isFavorite ? 'star' : 'star-o'" 
          :color="isFavorite ? '#ffcc00' : '#fff'"
          size="20"
        />
      </div>
    </div>
    
    <!-- 搭配信息 -->
    <div class="card-content">
      <h3 class="card-title">{{ outfit.name }}</h3>
      <div class="card-meta">
        <!-- 场合标签 -->
        <span v-if="outfit.occasions && outfit.occasions.length > 0" class="occasion-tag">
          {{ outfit.occasions.join('·') }}
        </span>
        <!-- 温度范围 -->
        <span v-if="outfit.minTemperature !== null || outfit.maxTemperature !== null" class="temperature-tag">
          {{ getTemperatureText() }}
        </span>
      </div>
      
      <!-- 衣物预览 -->
      <div class="clothes-preview" v-if="outfit.clothes && outfit.clothes.length > 0">
        <div 
          v-for="(clothes, index) in visibleClothes" 
          :key="clothes.id"
          class="clothes-thumbnail"
          :title="clothes.name"
        >
          <img :src="clothes.image" :alt="clothes.name" />
        </div>
        <div v-if="outfit.clothes.length > maxVisibleClothes" class="more-clothes">
          +{{ outfit.clothes.length - maxVisibleClothes }}
        </div>
      </div>
      
      <!-- 最后穿着时间 -->
      <div class="last-worn" v-if="outfit.lastWornDate">
        <van-icon name="clock-o" size="12" color="#999" /> 
        最后穿着: {{ formatDate(outfit.lastWornDate) }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { Icon } from 'vant';

// 定义props
const props = defineProps({
  // 穿搭数据
  outfit: {
    type: Object,
    required: true
  },
  // 最大显示的衣物数量
  maxVisibleClothes: {
    type: Number,
    default: 3
  }
});

// 定义事件
const emit = defineEmits(['click', 'toggleFavorite']);

// 默认封面图
const defaultCoverImage = 'https://dummyimage.com/600x400/eee/999&text=Outfit+Cover';

// 是否收藏
const isFavorite = ref(props.outfit.isFavorite || false);

// 可见的衣物列表
const visibleClothes = computed(() => {
  if (!props.outfit.clothes) return [];
  return props.outfit.clothes.slice(0, props.maxVisibleClothes);
});

// 点击事件处理
const onClick = () => {
  emit('click', props.outfit);
};

// 切换收藏状态
const toggleFavorite = () => {
  isFavorite.value = !isFavorite.value;
  emit('toggleFavorite', { ...props.outfit, isFavorite: isFavorite.value });
};

// 获取季节标签
const getSeasonLabel = (season) => {
  const seasonMap = {
    'spring': '春季',
    'summer': '夏季',
    'autumn': '秋季',
    'winter': '冬季',
    'all': '全年'
  };
  return seasonMap[season] || season;
};

// 获取场合标签
const getOccasionLabel = (occasion) => {
  const occasionMap = {
    'daily': '日常',
    'work': '工作',
    'party': '聚会',
    'sports': '运动',
    'formal': '正式场合',
    'other': '其他'
  };
  return occasionMap[occasion] || occasion;
};

// 获取温度文本
const getTemperatureText = () => {
  const { minTemperature, maxTemperature } = props.outfit;
  if (minTemperature !== null && maxTemperature !== null) {
    return `${minTemperature}°C - ${maxTemperature}°C`;
  } else if (minTemperature !== null) {
    return `≥ ${minTemperature}°C`;
  } else if (maxTemperature !== null) {
    return `≤ ${maxTemperature}°C`;
  }
  return '';
};

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return '';
  
  const date = new Date(dateString);
  const now = new Date();
  const diffTime = Math.abs(now - date);
  const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
  
  if (diffDays === 1) {
    return '昨天';
  } else if (diffDays < 7) {
    return `${diffDays}天前`;
  } else if (diffDays < 30) {
    const diffWeeks = Math.floor(diffDays / 7);
    return `${diffWeeks}周前`;
  } else if (diffDays < 365) {
    const diffMonths = Math.floor(diffDays / 30);
    return `${diffMonths}个月前`;
  } else {
    const diffYears = Math.floor(diffDays / 365);
    return `${diffYears}年前`;
  }
};
</script>

<style scoped>
.outfit-card {
  background-color: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  cursor: pointer;
  transition: all 0.3s ease;
}

.outfit-card:active {
  transform: scale(0.98);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.card-image-wrapper {
  position: relative;
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.card-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.outfit-card:hover .card-image {
  transform: scale(1.05);
}

.season-badge {
  position: absolute;
  top: 12px;
  left: 12px;
  background-color: rgba(255, 255, 255, 0.9);
  color: #333;
  padding: 4px 10px;
  border-radius: 16px;
  font-size: 12px;
  font-weight: 500;
}

.favorite-button {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 36px;
  height: 36px;
  background-color: rgba(0, 0, 0, 0.5);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.3s ease;
}

.favorite-button:active {
  background-color: rgba(0, 0, 0, 0.7);
}

.card-content {
  padding: 16px;
}

.card-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin: 0 0 10px 0;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-meta {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.occasion-tag,
.temperature-tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  background-color: #f0f7ff;
  color: #409eff;
}

.temperature-tag {
  background-color: #fff7e6;
  color: #ff9900;
}

.clothes-preview {
  display: flex;
  gap: 6px;
  margin-bottom: 10px;
}

.clothes-thumbnail {
  width: 36px;
  height: 36px;
  border-radius: 6px;
  overflow: hidden;
  border: 2px solid #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.clothes-thumbnail img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.more-clothes {
  width: 36px;
  height: 36px;
  border-radius: 6px;
  background-color: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #999;
  border: 2px solid #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.last-worn {
  font-size: 12px;
  color: #999;
  display: flex;
  align-items: center;
  gap: 4px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .card-image-wrapper {
    height: 160px;
  }
  
  .card-content {
    padding: 14px;
  }
  
  .card-title {
    font-size: 15px;
  }
  
  .clothes-thumbnail,
  .more-clothes {
    width: 32px;
    height: 32px;
  }
}

/* 暗黑模式适配 */
@media (prefers-color-scheme: dark) {
  .outfit-card {
    background-color: #1e1e1e;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  }
  
  .card-title {
    color: #f0f0f0;
  }
  
  .season-badge {
    background-color: rgba(0, 0, 0, 0.5);
    color: #fff;
  }
  
  .occasion-tag {
    background-color: rgba(64, 158, 255, 0.1);
    color: #66b1ff;
  }
  
  .temperature-tag {
    background-color: rgba(255, 153, 0, 0.1);
    color: #ffb74d;
  }
  
  .more-clothes {
    background-color: #2a2a2a;
    color: #707070;
  }
  
  .last-worn {
    color: #707070;
  }
}
</style>